package org.origin.ferret.center.configs;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

/**
 * 配置自定义 JsonMapper 配置
 *
 * @author ferret
 * @version 2024-05-08
 */
@Configuration
public class MyJsonConfig {

    @Bean
    @Primary
    @ConditionalOnMissingBean(ObjectMapper.class)
    public ObjectMapper jsonMapper(Jackson2ObjectMapperBuilder builder) {
        return builder.createXmlMapper(false).build();
    }

    // @Bean
    // @ConditionalOnMissingBean(XmlMapper.class)
    // public XmlMapper xmlMapper(Jackson2ObjectMapperBuilder builder) {
    //     XmlMapper xmlMapper = builder.createXmlMapper(true).build();
    //     // 是否设置外围标签名，默认true
    //     xmlMapper.setDefaultUseWrapper(false);
    //     // 字段为null，自动忽略，不再序列化
    //     xmlMapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
    //     // XML标签名:使用首字母大写骆驼命名的属性名，
    //     xmlMapper.setPropertyNamingStrategy(PropertyNamingStrategy.UPPER_CAMEL_CASE);
    //     // 直接输出原始的字段名
    //     xmlMapper.enable(MapperFeature.USE_STD_BEAN_NAMING);
    //     // 顺序输出
    //     xmlMapper.enable(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS);
    //     // 空值处理为空串
    //     xmlMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() {
    //         @Override
    //         public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
    //             jsonGenerator.writeString("");
    //         }
    //     });
    //     return xmlMapper;
    // }
}
